import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const router = new VueRouter({
    mode:'hash',
    routes:[
        {
            path:'/login',
            name:'login',
            component:()=>import('@/pages/login/Login.vue')
        },
        {
            path:'/home',
            name:'home',
            redirect:'/welcome',
            component:()=>import('@/pages/home/Home.vue'),
        //    http://localhost:8000/#/users
            children:[
                {
                    path:'/welcome',
                    component:()=>import('@/pages/home/welcome/Welcome.vue')
                },
                {
                    path:'/users',//  /home/users
                    name:'users',
                    component:()=>import('@/pages/home/users/Users.vue')
                },
                {
                    path:'/roles',
                    name:'roles',
                    component:()=>import('@/pages/home/rolesmanage/RolesList.vue')
                },
                {
                    path:'/rights',
                    name:'rights',
                    component:()=>import('@/pages/home/rolesmanage/RightsList.vue')
                },
                {
                    path:'/goods',
                    name:'goods',
                    component:()=>import('@/pages/home/goods/Goods.vue')
                },
                {
                    path:'/goodsinfo',
                    name:'goodsinfo',
                    component:()=>import('@/pages/home/goods/GoodsInfo.vue')
                },
                {
                    path:'/params',
                    name:'params',
                    component:()=>import('@/pages/home/goods/Params.vue')
                },
                {
                    path:'/categories',
                    name:'categories',
                    component:()=>import('@/pages/home/goods/Categories.vue')
                },
                {
                    path:'/orders',
                    name:'orders',
                    component:()=>import('@/pages/home/order/Orders.vue')
                },
                {
                    path:'/reports',
                    name:'reports',
                    component:()=>import('@/pages/home/reports/Reports.vue')
                }
            ]
        },
        {
            path:'/',
            redirect:'/login'
        }
    ]
})
/* 退出登录的权限处理
路由守卫的知识点
login  放行
路径不是login  看sessionStorage中有没有token 有放行  无 不放行去login */
router.beforeEach((to,from,next)=>{
    // console.log(to);
    // console.log(from);
    // 从login 跳转到login  to.path和from.path都指向 了login 错了  
    // 
    /* if(to.path != '/login'){
        if(window.sessionStorage.getItem('token')){
            next()
        }else{
            next('/login');
        }
    }
    next(); */
    if(to.path == '/login'){
        // 函数当中return的作用
        return next();
    }
    //不可能是login 
    if(!window.sessionStorage.getItem('token')){
        return next('/login');
    }
    next();



})
export default router;